package com.example.myapplication;

import android.content.Context;

import androidx.core.content.ContextCompat;

import com.example.myapplication.widgets.calendar.work.TimeWork;
import com.example.myapplication.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class TimeWorkUtils {

    public static final int[] ColorArray = new int[]{R.color.color_EF2F2F, R.color.color_FB5C2A
            , R.color.color_29C62F, R.color.color_6E6E6E};
    public static int Index = 0;

    // 转行成周日历所需的数据模型
    public static List<TimeWork> convertWeekTableData(Context context, TodoEntity todoEntity, String firstDate, String lastDate) {
        List<TimeWork> timeWorkList = new ArrayList<>();
        if (firstDate == null || lastDate == null) return timeWorkList;

        firstDate = firstDate.replace("/", "-");
        lastDate = lastDate.replace("/", "-");

        TimeWork timeWork;
        if (todoEntity.getEndTime() > 0) {
            String dateTime = TimeUtils.long2String(todoEntity.getEndTime(), "yyyy-MM-dd HH:mm:ss");
            String[] dateTimeSplit = dateTime.split(" ");
            if (dateTimeSplit[0].compareTo(firstDate) >= 0 && dateTimeSplit[0].compareTo(lastDate) <= 0) {
                // 日期在范围内
                timeWork = new TimeWork();
                timeWork.setTitle(todoEntity.getName());
                timeWork.setDate(dateTimeSplit[0]);
                timeWork.setTime(dateTimeSplit[1]);
                timeWork.setTip(dateTimeSplit[1].substring(0, dateTimeSplit[1].lastIndexOf(":")));
                timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                Index++;
                if (Index >= ColorArray.length)
                    Index = 0;
                timeWork.setId(todoEntity.getId());
                timeWorkList.add(timeWork);
            }
        } else {
            String startDateTime = todoEntity.getEndTimeRangeStart().replace("/", "-");
            String endDateTime = todoEntity.getEndTimeRangeEnd().replace("/", "-");
            String[] startDateTimeSplit = startDateTime.split(" ");
            String[] endDateTimeSplit = endDateTime.split(" ");
            if (startDateTimeSplit[0].equals(endDateTimeSplit[0])) {
                // 同一天
                if (startDateTimeSplit[0].compareTo(firstDate) >= 0 && startDateTimeSplit[0].compareTo(lastDate) <= 0) {
                    // 日期在范围内
                    String[] startTimeSplit = startDateTimeSplit[1].split(":");
                    String[] endTimeSplit = endDateTimeSplit[1].split(":");
                    try {
                        int startHour = Integer.parseInt(startTimeSplit[0]);
                        int endHour = Integer.parseInt(endTimeSplit[0]);
                        while (startHour <= endHour) {
                            timeWork = new TimeWork();
                            timeWork.setTitle(todoEntity.getName());
                            timeWork.setDate(startDateTimeSplit[0]);
                            timeWork.setTime(String.format(Locale.getDefault(), "%02d:00", startHour < 8 ? 0 : startHour));
                            timeWork.setTip(startDateTimeSplit[1].substring(0, startDateTimeSplit[1].lastIndexOf(":")) + "-" +
                                    endDateTimeSplit[1].substring(0, endDateTimeSplit[1].lastIndexOf(":")));
                            timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                            Index++;
                            if (Index >= ColorArray.length)
                                Index = 0;
                            timeWork.setId(todoEntity.getId());
                            timeWorkList.add(timeWork);

                            // 0-7在一个刻度内，无需重复添加
                            if (startHour < 8)
                                startHour = 8;
                            else
                                startHour++;
                        }
                    } catch (Exception ignored) {
                    }
                }
            } else {
                // 不是同一天
                String cycleDate = startDateTimeSplit[0];
                while (cycleDate.compareTo(endDateTimeSplit[0]) <= 0) {
                    String startTime, endTime;
                    int startHour, endHour;
                    try {
                        if (cycleDate.equals(startDateTimeSplit[0])) {
                            // 第一天，时间为开始时间到23:59:59
                            startTime = startDateTimeSplit[1].substring(0, startDateTimeSplit[1].lastIndexOf(":"));
                            endTime = "23:59";
                            startHour = Integer.parseInt(startDateTimeSplit[1].split(":")[0]);
                            endHour = 23;
                        } else if (cycleDate.equals(endDateTimeSplit[0])) {
                            // 最后一天，时间为00:00:00到结束时间
                            startTime = "00:00";
                            endTime = endDateTimeSplit[1].substring(0, endDateTimeSplit[1].lastIndexOf(":"));
                            startHour = 0;
                            endHour = Integer.parseInt(endDateTimeSplit[1].split(":")[0]);
                        } else {
                            startTime = "00:00";
                            endTime = "23:59";
                            startHour = 0;
                            endHour = 23;
                        }
                        while (startHour <= endHour) {
                            timeWork = new TimeWork();
                            timeWork.setTitle(todoEntity.getName());
                            timeWork.setDate(cycleDate);
                            timeWork.setTime(String.format(Locale.getDefault(), "%02d:00", startHour < 8 ? 0 : startHour));
                            timeWork.setTip(String.format(Locale.getDefault(), "%s-%s", startTime, endTime));
                            timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                            Index++;
                            if (Index >= ColorArray.length)
                                Index = 0;
                            timeWork.setId(todoEntity.getId());
                            timeWorkList.add(timeWork);

                            // 0-7在一个刻度内，无需重复添加
                            if (startHour < 8)
                                startHour = 8;
                            else
                                startHour++;
                        }
                    } catch (Exception ignored) {
                    }

                    cycleDate = TimeUtils.getNextDate(cycleDate);
                }
            }
        }

        return timeWorkList;
    }

    // 转行成月日历所需的数据模型
    public static List<TimeWork> convertMonthTableData(Context context, TodoEntity todoEntity, String firstDate, String lastDate) {
        List<TimeWork> timeWorkList = new ArrayList<>();
        if (firstDate == null || lastDate == null) return timeWorkList;

        firstDate = firstDate.replace("/", "-");
        lastDate = lastDate.replace("/", "-");

        TimeWork timeWork;
        if (todoEntity.getEndTime() > 0) {
            String dateTime = TimeUtils.long2String(todoEntity.getEndTime(), "yyyy-MM-dd HH:mm:ss");
            String[] dateTimeSplit = dateTime.split(" ");
            if (dateTimeSplit[0].compareTo(firstDate) >= 0 && dateTimeSplit[0].compareTo(lastDate) <= 0) {
                // 日期在范围内
                timeWork = new TimeWork();
                timeWork.setTitle(todoEntity.getName());
                timeWork.setDate(dateTimeSplit[0]);
                timeWork.setTip(dateTimeSplit[1].substring(0, dateTimeSplit[1].lastIndexOf(":")));
                timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                Index++;
                if (Index >= ColorArray.length)
                    Index = 0;
                timeWork.setId(todoEntity.getId());
                timeWorkList.add(timeWork);
            }
        } else {
            String startDateTime = todoEntity.getEndTimeRangeStart().replace("/", "-");
            String endDateTime = todoEntity.getEndTimeRangeEnd().replace("/", "-");
            String[] startDateTimeSplit = startDateTime.split(" ");
            String[] endDateTimeSplit = endDateTime.split(" ");
            if (startDateTimeSplit[0].equals(endDateTimeSplit[0])) {
                // 同一天
                if (startDateTimeSplit[0].compareTo(firstDate) >= 0 && startDateTimeSplit[0].compareTo(lastDate) <= 0) {
                    // 日期在范围内
                    timeWork = new TimeWork();
                    timeWork.setTitle(todoEntity.getName());
                    timeWork.setDate(startDateTimeSplit[0]);
                    timeWork.setTip(startDateTimeSplit[1].substring(0, startDateTimeSplit[1].lastIndexOf(":")) + "-" +
                            endDateTimeSplit[1].substring(0, endDateTimeSplit[1].lastIndexOf(":")));
                    timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                    Index++;
                    if (Index >= ColorArray.length)
                        Index = 0;
                    timeWork.setId(todoEntity.getId());
                    timeWorkList.add(timeWork);
                }
            } else {
                // 不是同一天
                String cycleDate = startDateTimeSplit[0];
                while (cycleDate.compareTo(endDateTimeSplit[0]) <= 0) {
                    if (cycleDate.compareTo(firstDate) >= 0 && cycleDate.compareTo(lastDate) <= 0) {
                        String startTime, endTime;
                        if (cycleDate.equals(startDateTimeSplit[0])) {
                            // 第一天，时间为开始时间到23:59:59
                            startTime = startDateTimeSplit[1].substring(0, startDateTimeSplit[1].lastIndexOf(":"));
                            endTime = "23:59";
                        } else if (cycleDate.equals(endDateTimeSplit[0])) {
                            // 最后一天，时间为00:00:00到结束时间
                            startTime = "00:00";
                            endTime = endDateTimeSplit[1].substring(0, endDateTimeSplit[1].lastIndexOf(":"));
                        } else {
                            startTime = "00:00";
                            endTime = "23:59";
                        }
                        timeWork = new TimeWork();
                        timeWork.setTitle(todoEntity.getName());
                        // todo 待修改正式数据
                        timeWork.setSubTitle("1/9");
                        timeWork.setDate(cycleDate);
                        timeWork.setTip(String.format(Locale.getDefault(), "%s-%s", startTime, endTime));
                        timeWork.setBgColor(ContextCompat.getColor(context, ColorArray[Index]));
                        Index++;
                        if (Index >= ColorArray.length)
                            Index = 0;
                        timeWork.setId(todoEntity.getId());
                        timeWorkList.add(timeWork);

                        cycleDate = TimeUtils.getNextDate(cycleDate);
                    } else
                        break;
                }
            }
        }

        return timeWorkList;
    }
}
